<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hsubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getrolelist } from '@/api/settings'
import { getimg } from '@/api/user'
import { assignRoles } from '@/api/employees'

export default {
  name: 'Yuyuyuyu',
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.doget()
  },

  methods: {
    closeDialog() {
      this.$emit('hsuccess')
    },
    // 数据回填
    async doget() {
      try {
        const res = await getrolelist()
        console.log('getrolelist', res)
        this.list = res.data.rows
        const red = await getimg(this.id)
        this.roleIds = red.data.roleIds
        // 保存数据
      } catch (err) {
        console.log('getrolelist', err)
      }
    },
    // 修改保存数据
    async doput() {
      try {
        const res = await assignRoles({ id: this.id, roleIds: this.roleIds })
        console.log('assignRoles', res)
        // 保存数据
      } catch (err) {
        console.log('assignRoles', err)
      }
    },
    hsubmit() {
      this.doput()
      this.$emit('hsuccess')
    }
  }
}
</script>
